package com.athena.view 
{
	import com.adobe.serialization.json.JSON;
	import com.athena.utils.Delegate;
	import com.athena.utils.Util;
	import com.junkbyte.console.Cc;
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author ...
	 */
	public class ChatRoom 
	{
		
		private var _movie : MovieClip;
		private var active : String = "";
		private var typeChat : String = "";
		
		private var _inTable : Boolean = false;
		private var _timeDelayChat : Timer;
		private var chat_txt : TextField;
		private var arrayContent : Array = ["", "", "", "", "", ""];//bàn, game, riêng, thế giới, he thong,  tất cả
		private var arrayContentIndex : Array = ["table", "game", "rieng", "thegioi", "tatca", "hethong"];
		public function ChatRoom() 
		{
			var cls : Class = Main.getInstance().loaderFile.queue.getLoader(Main.getInstance().config.dashboard.name).getClass("com.athena.ui.RoomChat") as Class;
			_movie = MovieClip(new cls); cls = null;
			_movie.x = -3.5; _movie.y = 158.5;
			
			chat_txt = new TextField();
			chat_txt.multiline = true;
			chat_txt.wordWrap = true;
			
			var textformat:TextFormat = new TextFormat();
			textformat.font = "Arial";
			textformat.size = 12;
			chat_txt.defaultTextFormat = textformat ;
			
			chat_txt.x = 14; chat_txt.y = 41;
			_movie.addChild(chat_txt);
			
			var style:StyleSheet = new StyleSheet();
 
			var hover:Object = new Object();
			hover.fontWeight = "bold";
			hover.color = "#3883cc";
			//hover.textDecoration = "underline";
			 
			style.setStyle("a:link", hover);
			style.setStyle("a:hover", hover);
			style.setStyle("a:active", hover);
			style.setStyle(".visited", hover);
			 
			chat_txt.styleSheet = style;
			
			setDefault();
		}
		
		
		private function setDefault() : void {
			var i : int = _movie.menu_mc.numChildren;
			for (var j : int = 0 ; j < i ; j++ ) {
				var _mc  : MovieClip = MovieClip(_movie.menu_mc.getChildAt(j));
				_mc.addEventListener(MouseEvent.ROLL_OVER, Delegate.create(onRollOverHandler, _mc.name));
				_mc.addEventListener(MouseEvent.ROLL_OUT, Delegate.create(onRollOutHandler, _mc.name));
				_mc.addEventListener(MouseEvent.CLICK, Delegate.create(onClickHandler, _mc.name));
			}
			
			_movie.addEventListener(KeyboardEvent.KEY_DOWN, sendChat);
			
			chat_txt.width = 251;
			chat_txt.height = 260;
			onClickHandler(null, "game");
		}
		
		private function sendChat(e : KeyboardEvent) : void {
			if (e.keyCode == Keyboard.ENTER) {
				sendChatServer();
			}
		}
		private function sendChatServer() : void {
			var _s  : String = _movie.input_txt.text;
			
			var pickRegExp:RegExp = /<|>/g;
			var str:String = _s.replace( pickRegExp, "*");
			str = Util.trim(str);
			if (str.length == 0) return;
			
			var _preChat : String = "";
			if (str.charAt(0) == "@") {
				typeChat = "rieng";
			}else {
				typeChat = (active == "all") ? typeChat : active; 
			}
			
			var obj : Object = new Object;
			
			//chat table
			if (typeChat == "table") {
				obj.evt = "chattable";
				obj.Name = Main.getInstance().currPlayer.displayName;
				obj.Data = str ;
				Main.getInstance().socket.sendData(JSON.encode(obj));
				_movie.input_txt.text = "";
				return;
			}
			var _sendToPlayer  : String;
			obj.evt = "16";
			
			if(typeChat == "game")
				obj.T = 2;
			else if (typeChat == "thegioi")
				obj.T = 1;
			else if (typeChat == "rieng") {
				obj.T = 4;
				if (!checkRuleChatSecret(str)) {
					return;
				}
				_sendToPlayer = str.substring(1, str.indexOf(" "));
				str = str.substring(str.indexOf(" ") + 1, str.length);
				str = Util.trim(str);
				if (str.length == 0) return;
			}
			if (str.length > 100) {
				str = str.substring(0, 100);
				str = Util.trim(str);
			}
			
			obj.D = str;
			var _v : String = (Main.getInstance().currPlayer.vip < 10) ? "#0" : "#";
			obj.N = Main.getInstance().currPlayer.displayName + _v + Main.getInstance().currPlayer.vip;
			
			if (typeChat == "game") {
				obj.G = Main.getInstance().gameObject.GAMEID;
			}
			

			if (typeChat == "rieng") {
				if (_inTable) {
					//var bool : Boolean = false;
					//for each(var pl : ObjectPlayer in ASBacay.gBoard.currentTable.arrPlayer) {
						//if (pl.PlayerName == _sendToPlayer) {
							//var	__strChat : String = "<font color='#FF0000'>Không thể chat riêng với người cùng bàn chơi!</font><br>" ;
							//chat_txt.htmlText = chat_txt.htmlText + __strChat;
							//scrollBottom();
							//return;
						//}
					//}
				}
				obj.NN = _sendToPlayer;
				_preChat = "@" + _sendToPlayer + " ";
			}
			if (!checkDelayChat()) return;
			Main.getInstance().socket.sendService(JSON.encode(obj));
			_movie.input_txt.text = _preChat;
		}
		
		private function checkDelayChat() : Boolean {
			return true;
			if (_timeDelayChat != null && _timeDelayChat.running) return false;
			_timeDelayChat = new Timer(1000, 1);
			_timeDelayChat.start();
			
			if (typeChat == "thegioi" && ASBacay.currPlayer.VIP < 7) {
				//addErrorChat("<font color=\"#FF0000\"> Bạn phải là Vip 7 trở lên mới được chat kênh TG</font>\n");
				//return false;
			}
			if (typeChat == "thegioi" || typeChat == "game") {
				var _t : int = 1;
				if (ASBacay.currPlayer.VIP == 10)
					_t = 3;
				if (ASBacay.currPlayer.VIP == 9)
					_t = 4;
				if (ASBacay.currPlayer.VIP == 8)
					_t = 4;
				if (ASBacay.currPlayer.VIP == 7)
					_t = 4;
				if (ASBacay.currPlayer.VIP == 6)
					_t = 5;
				if (ASBacay.currPlayer.VIP == 5)
					_t = 5;
				if (ASBacay.currPlayer.VIP == 4)
					_t = 6;
				if (ASBacay.currPlayer.VIP == 3)
					_t = 6;
				if (ASBacay.currPlayer.VIP == 2)
					_t = 120;
				if (ASBacay.currPlayer.VIP == 1)
					_t = 120;
				if (ASBacay.currPlayer.VIP == 0)
					_t = 120;
				if (ASBacay._timeDelayTGChat != null && ASBacay._timeDelayTGChat.running) {
					addErrorChat("<font color=\"#FF0000\"> Bạn chat nhanh quá, nghỉ tay uống miếng nước nào!</font>\n");
					return false;
				}
				_timeDelayTGChat = new Timer(_t * 1000 , 1);
				_timeDelayTGChat.start();
			}
			return true;
		}
		
		private function checkRuleChatSecret(_str : String) : Boolean {
			var _chk : Boolean = true;
			if (_str.charAt(0) != "@") _chk = false;
			var _fSpace : int = _str.indexOf(" ");
			if (_fSpace == 1 || _fSpace == -1) _chk = false;
			if (!_chk)
				addErrorChat("<font color=\"#FF0000\"> Chat riêng : </font><font color=\"#3883cc\">@[ten] [ ] [noidung]</font><br>");
			return _chk;
		}
		
		private function addErrorChat(str : String) : void {
			
		}
		private function onRollOverHandler(e : MouseEvent, _name : String) : void {
			Mouse.cursor = MouseCursor.BUTTON;
			if(active != _name)
				(e.currentTarget as MovieClip).gotoAndStop(2);
		}
		private function onRollOutHandler(e : MouseEvent, _name : String) : void {
			Mouse.cursor = MouseCursor.AUTO;
			if (active != _name)
				(e.currentTarget as MovieClip).gotoAndStop(1);
		}
		
		public function addTextChat(obj : Object) : void {
			var _strname: String = obj.N;
			var _name : String = _strname.slice(0, _strname.length - 3);
			var _vip : int = parseInt(_strname.slice(_strname.length - 2, _strname.length));
			var _color : String;
			var _strChat : String;
			if (obj.T == 2) {
				_color = "#FFCC00";
			}else if (obj.T == 1) {
				_color = "#F6EF87";
			}else if (obj.T == 4) {
				_color = "#E78035";
				if (obj.N == "Hệ thống") {
					_name = obj.N;
					_color = "#FF0000";
					_strChat = "<font color='" + _color + "'>[<a href=\"event:" + _name + "\">" + _name + "</a>]: " + obj.D +  "</font><br>" ;
					chat_txt.htmlText = chat_txt.htmlText + _strChat;
					scrollBottom();
					return;
				}
			}
			_strChat =  "<font color='" + _color + "'>V" + _vip +  "-[<a href=\"event:" + _name + "\">" + _name + "</a>]: " + obj.D +  "</font><br>" ;
			
			if (obj.T == 2) {
				arrayContent[1] = arrayContent[1] + _strChat;
			}else if (obj.T == 1) {
				arrayContent[3] = arrayContent[3] + _strChat;
			}else if (obj.T == 4) {
				if (obj.N == "Hệ thống") {
					arrayContent[5] = arrayContent[5] + _strChat;
				}else {
					arrayContent[2] = arrayContent[2] + _strChat;
					if(active != "rieng"){
					}
				}
			}
			arrayContent[4] = arrayContent[4] + _strChat;
			updateChat();
		}
		
		public function updateChat() : void {
			var ind : int = arrayContentIndex.indexOf(active) != -1 ? arrayContentIndex.indexOf(active) : 5;
			chat_txt.htmlText = arrayContent[ind];
			scrollBottom();
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		private function scrollBottom() : void {
			
		}
		private function onClickHandler(e : MouseEvent, _name : String) : void {
			if (active == _name) return;
			active = _name;
			
			var i : int = _movie.menu_mc.numChildren ;
			for (var j : int = 0 ; j < i ; j++ ) {
				var _mc  : MovieClip = MovieClip(_movie.menu_mc.getChildAt(j));
				
				if (_mc.name != _name) _mc.gotoAndStop(1);
				else {
					_mc.gotoAndStop(3);
				}
			}
			switch(active) {
				case "thegioi" :
					_movie.type_txt.text = "TG";
					typeChat = active;
					break;
				case "game" :
					_movie.type_txt.text = "Game";
					typeChat = active;
					break;
				case "rieng" :
					_movie.type_txt.text = "Riêng";
					typeChat = active;
					break;
				case "all" :
					break;
			}
			Main.getInstance().stage.focus = _movie.input_txt;
			updateChat();
		}
		public function get movie():MovieClip 
		{
			return _movie;
		}
		
	}

}